﻿<%@ Page Title="Manage Applications" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="ManageApplications.aspx.cs" Inherits="NIntegrate.Configuration.UI.ManageApplications" MaintainScrollPositionOnPostback="true" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <h2>Manage Applications</h2>
    <div class="bottomhyperlink"><img alt="Add new application" src="Images/plus.gif" /><asp:LinkButton 
            ID="btnShowAddNewPanel" runat="server" Text="Add new application" 
            onclick="btnShowAddNewPanel_Click"></asp:LinkButton></div>
    <br />
    <asp:GridView CssClass="gridview"
        ID="gvApps" runat="server" 
        DataSourceID="dsApps"
        DataKeyNames="AppCode"
        AllowSorting="true"
        AutoGenerateDeleteButton="true"
        AutoGenerateColumns="false"
        AutoGenerateEditButton="true" onrowcommand="gvApps_RowCommand">
        <Columns>
            <asp:TemplateField HeaderText="AppCode" SortExpression="AppCode">
                <ItemTemplate>
                    <%#Eval("AppCode")%>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="tbAppCode" runat="server" MaxLength="10" Text='<%#Bind("AppCode") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="tbAppCodeRequired" runat="server"
                        ControlToValidate="tbAppCode" EnableClientScript="false"
                        ErrorMessage="Required"></asp:RequiredFieldValidator>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Name" SortExpression="Name">
                <ItemTemplate>
                    <%#Eval("Name") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="tbName" runat="server" MaxLength="100" Text='<%#Bind("Name") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="tbNameRequired" runat="server"
                        ControlToValidate="tbName" EnableClientScript="false"
                        ErrorMessage="Required"></asp:RequiredFieldValidator>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:BoundField HeaderText="Description" DataField="Description" SortExpression="Description" />
            <asp:TemplateField HeaderText="AppVariables">
                <ItemTemplate>
                    <asp:LinkButton ID="btnViewAppVariables" runat="server" Text="Manage AppVariables" CommandName="Select" CommandArgument='<%# "ViewAppVariables|" + Eval("AppCode") %>'></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Services">
                <ItemTemplate>
                    <asp:LinkButton ID="btnViewServices" runat="server" Text="Manage Services" CommandName="Select" CommandArgument='<%# "ViewServices|" + Eval("AppCode") %>'></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <SelectedRowStyle BackColor="LightYellow" />
    </asp:GridView>
    <asp:Panel ID="panelBottom" runat="server" CssClass="panelBottom">
        <asp:DetailsView ID="dvAddApp" runat="server" Visible="false"
            DataSourceID="dsApps" DefaultMode="Insert"
            AutoGenerateRows="false"
            AutoGenerateInsertButton="true"
            CssClass="detailstable" onitemcommand="dvAddApp_ItemCommand" 
            oniteminserted="dvAddApp_ItemInserted">
            <Fields>
                <asp:TemplateField HeaderText="AppCode">
                    <InsertItemTemplate>
                        <asp:TextBox ID="tbAppCode" runat="server" MaxLength="10" Text='<%#Bind("AppCode") %>'></asp:TextBox>
                        <asp:RequiredFieldValidator ID="tbAppCodeRequired" runat="server"
                            ControlToValidate="tbAppCode" EnableClientScript="false"
                            ErrorMessage="Required"></asp:RequiredFieldValidator>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <InsertItemTemplate>
                        <asp:TextBox ID="tbName" runat="server" MaxLength="100" Text='<%#Bind("Name") %>'></asp:TextBox>
                        <asp:RequiredFieldValidator ID="tbNameRequired" runat="server"
                            ControlToValidate="tbName" EnableClientScript="false"
                            ErrorMessage="Required"></asp:RequiredFieldValidator>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:BoundField HeaderText="Description" DataField="Description" SortExpression="Description" />
            </Fields>
        </asp:DetailsView>
        <asp:Panel ID="panelAppVariables" runat="server" Visible="false">
            <div class="bottomhyperlink"><img alt="Add new appvariable" src="Images/plus.gif" /><asp:LinkButton 
                    ID="btnShowSubAddNewPanel" runat="server" Text="Add new appvariable" 
                    onclick="btnShowSubAddNewPanel_Click"></asp:LinkButton></div>
            <br />
            <asp:GridView CssClass="gridview"
                ID="gvAppVariables" runat="server" 
                DataSourceID="dsAppVariables"
                DataKeyNames="AppCode,AppVariableName,Environment_id"
                AllowSorting="true"
                AutoGenerateDeleteButton="true"
                AutoGenerateColumns="false"
                EmptyDataText="No AppVariables for selected application"
                AutoGenerateEditButton="true" onrowcommand="gvAppVariables_RowCommand">
                <Columns>
                    <asp:TemplateField HeaderText="AppVariableName" SortExpression="AppVariableName">
                        <ItemTemplate>
                            <%#Eval("AppVariableName")%>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="tbAppVariableName" runat="server" Text='<%#Bind("AppVariableName") %>'></asp:TextBox>
                            <asp:RequiredFieldValidator ID="tbAppVariableNameRequired" runat="server"
                                ControlToValidate="tbAppVariableName" EnableClientScript="false"
                                ErrorMessage="Required"></asp:RequiredFieldValidator>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Application">
                        <ItemTemplate>
                            <asp:DropDownList ID="ddlApps" runat="server" SelectedValue='<%# Bind("AppCode") %>' DataSourceID="dsApps" DataTextField="Name" DataValueField="AppCode" Enabled="false"></asp:DropDownList>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlApps" runat="server" SelectedValue='<%# Bind("AppCode") %>' DataSourceID="dsApps" DataTextField="Name" DataValueField="AppCode"></asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Environment" SortExpression="Environment_id">
                        <ItemTemplate>
                            <asp:DropDownList ID="ddlEnvironments" runat="server" SelectedValue='<%# Bind("Environment_id") %>' DataSourceID="dsEnvironments" DataTextField="Name" DataValueField="Environment_id" Enabled="false"></asp:DropDownList>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlEnvironments" runat="server" SelectedValue='<%# Bind("Environment_id") %>' DataSourceID="dsEnvironments" DataTextField="Name" DataValueField="Environment_id"></asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Value" SortExpression="Value">
                        <ItemTemplate>
                            <%#Eval("Value")%>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="tbValue" runat="server" Text='<%#Bind("Value") %>'></asp:TextBox>
                            <asp:RequiredFieldValidator ID="tbValueRequired" runat="server"
                                ControlToValidate="tbValue" EnableClientScript="false"
                                ErrorMessage="Required"></asp:RequiredFieldValidator>
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <asp:Panel ID="panelAddAppVariable" runat="server" CssClass="panelBottom" Visible="false">
                <asp:DetailsView ID="dvAddAppVariable" runat="server"
                    DataSourceID="dsAppVariables" DefaultMode="Insert"
                    AutoGenerateRows="false"
                    AutoGenerateInsertButton="true"
                    CssClass="detailstable" onitemcommand="dvAddAppVariable_ItemCommand" 
                    oniteminserted="dvAddAppVariable_ItemInserted" 
                    ondatabound="dvAddAppVariable_DataBound">
                    <Fields>
                        <asp:TemplateField HeaderText="AppVariableName">
                            <InsertItemTemplate>
                                <asp:TextBox ID="tbAppVariableName" runat="server" Text='<%#Bind("AppVariableName") %>'></asp:TextBox>
                                <asp:RequiredFieldValidator ID="tbAppVariableNameRequired" runat="server"
                                    ControlToValidate="tbAppVariableName" EnableClientScript="false"
                                    ErrorMessage="Required"></asp:RequiredFieldValidator>
                            </InsertItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Application">
                            <InsertItemTemplate>
                                <asp:DropDownList ID="ddlApps" runat="server" SelectedValue='<%# Bind("AppCode") %>' DataSourceID="dsApps" DataTextField="Name" DataValueField="AppCode"></asp:DropDownList>
                            </InsertItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Environment">
                            <InsertItemTemplate>
                                <asp:DropDownList ID="ddlEnvironments" runat="server" SelectedValue='<%# Bind("Environment_id") %>' DataSourceID="dsEnvironments" DataTextField="Name" DataValueField="Environment_id"></asp:DropDownList>
                            </InsertItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Value">
                            <InsertItemTemplate>
                                <asp:TextBox ID="tbValue" runat="server" Text='<%#Bind("Value") %>'></asp:TextBox>
                                <asp:RequiredFieldValidator ID="tbValueRequired" runat="server"
                                    ControlToValidate="tbValue" EnableClientScript="false"
                                    ErrorMessage="Required"></asp:RequiredFieldValidator>
                            </InsertItemTemplate>
                        </asp:TemplateField>
                    </Fields>
                </asp:DetailsView>            
            </asp:Panel>
        </asp:Panel>
        <asp:Panel ID="panelServices" runat="server" Visible="false">
            <div class="bottomhyperlink"><img alt="Add new service" src="Images/plus.gif" /><asp:LinkButton 
                    ID="LinkButton1" runat="server" Text="Add new service" 
                    onclick="btnShowSubAddNewPanel2_Click"></asp:LinkButton></div>
            <br />
            <asp:GridView CssClass="gridview"
                ID="gvServices" runat="server" 
                DataSourceID="dsServices"
                DataKeyNames="Service_id"
                AllowSorting="true"
                AutoGenerateDeleteButton="true"
                AutoGenerateColumns="false"
                EmptyDataText="No Services for selected application"
                AutoGenerateEditButton="true" onrowcommand="gvServices_RowCommand">
                <Columns>
                    <asp:TemplateField HeaderText="ServiceName" SortExpression="ServiceName">
                        <ItemTemplate>
                            <%#Eval("ServiceName")%>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="tbServiceName" runat="server" Text='<%#Bind("ServiceName") %>'></asp:TextBox>
                            <asp:RequiredFieldValidator ID="tbServiceNameRequired" runat="server"
                                ControlToValidate="tbServiceName" EnableClientScript="false"
                                ErrorMessage="Required"></asp:RequiredFieldValidator>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Application">
                        <ItemTemplate>
                            <asp:DropDownList ID="ddlApps" runat="server" SelectedValue='<%# Bind("AppCode") %>' DataSourceID="dsApps" DataTextField="Name" DataValueField="AppCode" Enabled="false"></asp:DropDownList>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlApps" runat="server" SelectedValue='<%# Bind("AppCode") %>' DataSourceID="dsApps" DataTextField="Name" DataValueField="AppCode"></asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>                    
                    <asp:TemplateField HeaderText="ServiceCategry" SortExpression="ServiceCategory_id">
                        <ItemTemplate>
                            <asp:DropDownList ID="ddlServiceCategories" runat="server" SelectedValue='<%# Bind("ServiceCategory_id") %>' DataSourceID="dsServiceCategories" DataTextField="ServiceCategoryName" DataValueField="ServiceCategory_id" Enabled="false"></asp:DropDownList>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlServiceCategories" runat="server" SelectedValue='<%# Bind("ServiceCategory_id") %>' DataSourceID="dsServiceCategories" DataTextField="ServiceCategoryName" DataValueField="ServiceCategory_id"></asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Behavior" SortExpression="ServiceBehavior_id">
                        <ItemTemplate>
                            <asp:DropDownList ID="ddlServiceBehaviors" runat="server" SelectedValue='<%# Bind("ServiceBehavior_id") %>' Enabled="false" DataTextField="BehaviorName" DataValueField="Behavior_id" DataSourceID="dsServiceBehaviors" AppendDataBoundItems="true">
                                <asp:ListItem Text="None" Value=""></asp:ListItem>
                            </asp:DropDownList>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlServiceBehaviors" runat="server" SelectedValue='<%# Bind("ServiceBehavior_id") %>' DataTextField="BehaviorName" DataValueField="Behavior_id" DataSourceID="dsServiceBehaviors" AppendDataBoundItems="true">
                                <asp:ListItem Text="None" Value=""></asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>  
                    <asp:TemplateField HeaderText="ServiceHostType" SortExpression="ServiceHostType_id">
                        <ItemTemplate>
                            <asp:DropDownList ID="ddlServiceHostTypes" runat="server" SelectedValue='<%# Bind("ServiceHostType_id") %>' Enabled="false" DataTextField="ServiceHostTypeFriendlyName" DataValueField="ServiceHostType_id" DataSourceID="dsServiceHostTypes">
                            </asp:DropDownList>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlServiceHostTypes" runat="server" SelectedValue='<%# Bind("ServiceHostType_id") %>' DataTextField="ServiceHostTypeFriendlyName" DataValueField="ServiceHostType_id" DataSourceID="dsServiceHostTypes">
                            </asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>  
                    <asp:TemplateField HeaderText="HostXML" SortExpression="HostXML">
                        <ItemTemplate>
                            <%# Eval("HostXML") == DBNull.Value ? string.Empty : Server.HtmlEncode((string)Eval("HostXML"))%>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="tbHostXML" runat="server" Text='<%#Bind("HostXML") %>' TextMode="MultiLine" Rows="3"></asp:TextBox>                   
                        </EditItemTemplate>
                    </asp:TemplateField>                    
                </Columns>
            </asp:GridView>
            <asp:Panel ID="panelAddService" runat="server" CssClass="panelBottom" Visible="false">
                <asp:DetailsView ID="dvAddService" runat="server"
                    DataSourceID="dsServices" DefaultMode="Insert"
                    AutoGenerateRows="false"
                    AutoGenerateInsertButton="true"
                    CssClass="detailstable" onitemcommand="dvAddService_ItemCommand" 
                    oniteminserted="dvAddService_ItemInserted" 
                    ondatabound="dvAddService_DataBound">
                    <HeaderTemplate>
                        <h5>XML Templates:</h5>
                        <a target="_blank" href="XmlTemplates/Host.xml">Host</a>
                    </HeaderTemplate>                                
                    <Fields>
                        <asp:TemplateField HeaderText="ServiceName">
                            <InsertItemTemplate>
                                <asp:TextBox ID="tbServiceName" runat="server" Text='<%#Bind("ServiceName") %>'></asp:TextBox>
                                <asp:RequiredFieldValidator ID="tbServiceNameRequired" runat="server"
                                    ControlToValidate="tbServiceName" EnableClientScript="false"
                                    ErrorMessage="Required"></asp:RequiredFieldValidator>
                            </InsertItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Application">
                            <InsertItemTemplate>
                                <asp:DropDownList ID="ddlApps" runat="server" SelectedValue='<%# Bind("AppCode") %>' DataSourceID="dsApps" DataTextField="Name" DataValueField="AppCode"></asp:DropDownList>
                            </InsertItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="ServiceCategry">
                            <InsertItemTemplate>
                                <asp:DropDownList ID="ddlServiceCategories" runat="server" SelectedValue='<%# Bind("ServiceCategory_id") %>' DataSourceID="dsServiceCategories" DataTextField="ServiceCategoryName" DataValueField="ServiceCategory_id"></asp:DropDownList>
                            </InsertItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Behavior">
                            <InsertItemTemplate>
                                <asp:DropDownList ID="ddlServiceBehaviors" runat="server" SelectedValue='<%# Bind("ServiceBehavior_id") %>' DataTextField="BehaviorName" DataValueField="Behavior_id" DataSourceID="dsServiceBehaviors" AppendDataBoundItems="true">
                                    <asp:ListItem Text="None" Value=""></asp:ListItem>
                                </asp:DropDownList>
                            </InsertItemTemplate>
                        </asp:TemplateField>  
                        <asp:TemplateField HeaderText="ServiceHostType">
                            <InsertItemTemplate>
                                <asp:DropDownList ID="ddlServiceHostTypes" runat="server" SelectedValue='<%# Bind("ServiceHostType_id") %>' DataTextField="ServiceHostTypeFriendlyName" DataValueField="ServiceHostType_id" DataSourceID="dsServiceHostTypes">
                                </asp:DropDownList>
                            </InsertItemTemplate>
                        </asp:TemplateField>  
                        <asp:TemplateField HeaderText="HostXML">
                            <InsertItemTemplate>
                                <asp:TextBox ID="tbHostXML" runat="server" Text='<%#Bind("HostXML") %>' TextMode="MultiLine" Rows="3"></asp:TextBox>                   
                            </InsertItemTemplate>
                        </asp:TemplateField> 
                    </Fields>
                </asp:DetailsView>            
            </asp:Panel>
        </asp:Panel>
    </asp:Panel>
    <asp:HiddenField ID="hidSelectedAppCode" runat="server" />    
    <ni:QueryDataSource ID="dsApps" runat="server" UseLocalQueryService="true">
    </ni:QueryDataSource>
    <ni:QueryDataSource ID="dsEnvironments" runat="server" UseLocalQueryService="true">
    </ni:QueryDataSource>
    <ni:QueryDataSource ID="dsAppVariables" runat="server" UseLocalQueryService="true">
    </ni:QueryDataSource>
    <ni:QueryDataSource ID="dsServices" runat="server" UseLocalQueryService="true">
    </ni:QueryDataSource>
    <ni:QueryDataSource ID="dsServiceCategories" runat="server" UseLocalQueryService="true">
    </ni:QueryDataSource>
    <ni:QueryDataSource ID="dsServiceBehaviors" runat="server" UseLocalQueryService="true">
    </ni:QueryDataSource>
    <ni:QueryDataSource ID="dsServiceHostTypes" runat="server" UseLocalQueryService="true">
    </ni:QueryDataSource>    
</asp:Content>
